package com.tabjin.rfidsocket.service.sys.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.tabjin.rfidsocket.common.RequestHolder;
import com.tabjin.rfidsocket.mapper.mapper.SysRoleUserCustomMapper;
import com.tabjin.rfidsocket.mapper.mapper.SysRoleUserMapper;
import com.tabjin.rfidsocket.mapper.mapper.SysUserMapper;
import com.tabjin.rfidsocket.pojo.SysRole;
import com.tabjin.rfidsocket.pojo.SysRoleUser;
import com.tabjin.rfidsocket.pojo.SysUser;
import com.tabjin.rfidsocket.service.sys.SysRoleUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author tabjin
 * create at 2020/12/28 09:49
 * @program rfid_socket
 * @description
 */
@Service
public class SysRoleUserServiceImpl implements SysRoleUserService {

    private final SysRoleUserMapper sysRoleUserMapper;
    private final SysUserMapper sysUserMapper;
    private final SysRoleUserCustomMapper sysRoleUserCustomMapper;

    public SysRoleUserServiceImpl(SysRoleUserMapper sysRoleUserMapper, SysUserMapper sysUserMapper, SysRoleUserCustomMapper sysRoleUserCustomMapper) {
        this.sysRoleUserMapper = sysRoleUserMapper;
        this.sysUserMapper = sysUserMapper;
        this.sysRoleUserCustomMapper = sysRoleUserCustomMapper;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SysUser> getUserListByRoleId(Integer roleId) {
        List<Integer> userIdList = sysRoleUserMapper.getUserIdListByRoleId(roleId);
        if (CollectionUtils.isEmpty(userIdList)) {
            return Lists.newArrayList();
        }
        // 角色选中的用户 userIdList -> userList
        return sysUserMapper.getUserByUserIdList(userIdList);
    }

    /**
     * 更改角色用户
     * @param roleId
     * @param userIdList
     */
    @Override
    public void changeRoleUsers(Integer roleId, List<Integer> userIdList) {
        List<Integer> originUserIdList = sysRoleUserCustomMapper.getUserIdListByRoleId(roleId);
        // 判断是否需要更改
        if (originUserIdList.size() == userIdList.size()) {
            // List 转 Set
            Set<Integer> originUserIdSet = Sets.newHashSet(originUserIdList);
            Set<Integer> userIdSet = Sets.newHashSet(userIdList);
            originUserIdSet.removeAll(userIdSet);
            if (CollectionUtils.isEmpty(originUserIdSet)) {
                return;
            }
        }
        updateRoleUsers(roleId, userIdList);
    }

    /**
     * 更新角色和用户
     *
     * @param roleId
     * @param userIdList
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateRoleUsers(Integer roleId, List<Integer> userIdList) {
        // 直接删掉该角色下的所有再重新注入
        sysRoleUserCustomMapper.deleteByRoleId(roleId);

        if (CollectionUtils.isEmpty(userIdList)) {
            return;
        }

        List<SysRoleUser> sysRoleUserList = Lists.newArrayList();
        for (Integer userId : userIdList) {
            SysRoleUser sysRoleUser = SysRoleUser.builder()
                    .roleId(roleId)
                    .userId(userId)
                    .operator(RequestHolder.getCurrentUser().getUsername())
                    .operateIp("127.0.0.1")
                    .operateTime(new Date())
                    .build();
            sysRoleUserList.add(sysRoleUser);
        }

        sysRoleUserCustomMapper.batchInsert(sysRoleUserList);
    }
}
